import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';

import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';

import set from '@dfeidao/atom-web/local/set';
import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';
import show from '@dfeidao/atom-web/ui/show';
import p001 from './p001';
import p002 from './p002';
import p003 from './p003';
import p004 from './p004';

export default async function a001(fd: IFeidaoAiBrowserComponent, users1: {}, no: string, staff_no: string, staff_name: string) {
	// todo
	const up = fd.data.node.querySelector<HTMLDivElement>('[data-id="kaoShi"]');
	set_node_cls(up, 'display-n', false);
	show(fd.data.node);
	const res = await nodejs<{ dt: Array<{ exam_score: string, exam_name: string, exam_time: number }> }>('employee-examination/zj-000003/s001',
		{
			filter: {
				exam_id: no
			}
		});
	const dt = res.dt.map((n) => {
		n.exam_time = res.dt[0].exam_time * 60;
		return n;
	});
	// 考试时长
	set(fd, 'dtTime', dt);
	// 总分
	set(fd, 'exam_score', res.dt[0].exam_score);
	render(fd, dt, p001, 'p001', 'replace');
	const res1 = await nodejs<{ dt: Array<{}>, c: number, dt1: Array<{}>, c1: number, dt2: Array<{}>, c2: number }>('employee-examination/zj-000003/s003', {});
	// 单选题
	const a = Math.round(res1.c / 10);
	const b = (Math.random() * 1000000000000 % a) + 1;
	const str = Math.round(b);
	const res2 = await nodejs<{ dt: Array<{ right_answer: string, question_no: string }> }>('employee-examination/zj-000003/s003',
		{
			filter: {
				pn: str
			}
		});
	const arr = Array();
	for (let i = 0; i < res2.dt.length; i++) {
		arr.push(res2.dt[i].question_no);
	}
	if (res2.dt.length < 9 && str !== 1) {
		const re = await nodejs<{ dt: Array<{ question_no: string }> }>('employee-examination/zj-000003/s002',
			{
				filter: {
					pn: (str - 1)
				}
			});
		for (let i = 0; i < re.dt.length; i++) {
			if (arr.length < 10) {
				arr.push(re.dt[i].question_no);
			}
		}
	}
	// 多选题
	const a1 = Math.round(res1.c1 / 5);
	const b1 = (Math.random() * 1000000000000 % a1) + 1;
	const str1 = Math.round(b1);
	const res3 = await nodejs<{ dt: Array<{ question_no: string }> }>('employee-examination/zj-000003/s004',
		{
			filter: {
				pn: str1
			}
		});
	const arr1 = Array();
	for (let i = 0; i < res3.dt.length; i++) {
		arr1.push(res3.dt[i].question_no);
	}
	if (res3.dt.length < 4 && str1 !== 1) {
		const re = await nodejs<{ dt: Array<{ question_no: string }> }>('employee-examination/zj-000003/s004',
			{
				filter: {
					pn: (str1 - 1)
				}
			});
		for (let i = 0; i < re.dt.length; i++) {
			if (arr1.length < 5) {
				arr1.push(re.dt[i].question_no);
			}
		}
	}
	// 判断题
	const a2 = Math.round(res1.c1 / 5);
	const b2 = (Math.random() * 1000000000000 % a2) + 1;
	const str2 = Math.round(b2);
	const res4 = await nodejs<{ dt: Array<{ question_no: string }> }>('employee-examination/zj-000003/s005',
		{
			filter: {
				pn: str2
			}
		});
	const arr2 = Array();
	for (let i = 0; i < res4.dt.length; i++) {
		arr2.push(res4.dt[i].question_no);
	}
	if (res4.dt.length < 4 && str2 !== 1) {
		const re = await nodejs<{ dt: Array<{ question_no: string }> }>('employee-examination/zj-000003/s005',
			{
				filter: {
					pn: (str2 - 1)
				}
			});
		for (let i = 0; i < re.dt.length; i++) {
			if (arr2.length < 5) {
				arr2.push(re.dt[i].question_no);
			}
		}
	}
	// 渲染试卷  单选
	const res5 = await nodejs<{ dt: Array<{ right_answer: string, topic_type: string, question_no: string, length1: number }>, dt1: Array<{ topic_type: string, question_no: string, length1: number }>, dt2: Array<{ topic_type: string, question_no: string, length1: number }> }>('employee-examination/zj-000003/s006',
		{
			filter: {
				question_no: arr,// 单选题
				question_no1: arr1,// 多选题
				question_no2: arr2// 判断题
			}
		});
	for (let i = 0; i < res5.dt.length; i++) {
		res5.dt[i].length1 = i + 1;
	}
	render(fd, res5.dt, p002, 'p002', 'inner');
	// 渲染试卷  多选
	for (let i = 0; i < res5.dt1.length; i++) {
		res5.dt1[i].length1 = i + 11;
	}
	render(fd, res5.dt1, p003, 'p003', 'inner');
	// 渲染试卷  判断
	for (let i = 0; i < res5.dt2.length; i++) {
		res5.dt2[i].length1 = i + 16;
	}
	render(fd, res5.dt2, p004, 'p004', 'inner');
	// 试题编号
	const arr3 = Array();
	arr3.push(arr, arr1, arr2);
	set(fd, 'no', arr3);
	set(fd, 'dt', res5.dt);
	set(fd, 'dt1', res5.dt1);
	set(fd, 'dt2', res5.dt2);
	// 考试编号：
	set(fd, 'exam_id', no);
	// 考生编号:
	set(fd, 'staff_no', staff_no);
	fire("zj-000003", "a008");
}
